import axios from 'axios'
import { MessageBox } from 'element-ui'
import AppConst from '@/constants/AppConst'

const http = axios.create({
  baseURL: AppConst.baseUrl,
  withCredentials: true,
  // timeout: 5 * 60 * 1000,
  timeout: 30 * 1000
  // headers: {'X-Custom-Header': 'foobar'}
})

http.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  })

/** **** respone拦截器==>对响应做处理 ******/
http.interceptors.response.use(
  response => {
    const result = response.data
    /* if (result.errorMsg) {
          MessageBox.alert(result.errorMsg)
        }*/
    return result
  },
  error => {
    if (error.response) {
      MessageBox.alert(error.response.data.errorMsg)
      // console.log(error.response.data.errorMsg)
      // console.log(123)
      switch (error.response.status) {
        case 601:
        case 602:
          console.log(123)
          const result = error.response.data
          if (result) {
            // @ts-ignore
            alert(result.errorMsg)
            return Promise.reject(error.response)
          } else {
            // @ts-ignore
            alert(error.response)
          }
          console.log(123)
          break
        case 603:
          break
      }
      return Promise.reject(error.response) // 返回接口返回的错误信息
    }
    return Promise.reject(error)
  }
)
export default http
